/**
 * by samsun
 */
var epcVersioninfoStore = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({	//proxy从哪里获取数据
		url : 'showEpcunitsVersionInfo.action?method=showEpcunitsVersionInfo',
		method : 'post'
	}),
	reader : new Ext.data.JsonReader({	//reader如何解析数据
		root : 'versionInfo',
		totalProperty : 'totalVersionInfoSize',
		id : 'version'
	}, [ {
		name : 'groupId'
	}, {
		name : 'time'
	},{
		name : 'author'
	}, {
		name : 'versionId'
	}
	])
});
//版本表格列模型
var epcVersioninfosm=new Ext.grid.CheckboxSelectionModel ({singleSelect : false});
var versioninfoColumnModel=new Ext.grid.ColumnModel(
		[epcVersioninfosm,
		 new Ext.grid.RowNumberer(),
		 {
			header : '版本号',
			width : 100,
			dataIndex : 'versionId',
			sortable : true,
			align : 'center'
		 },   {
			 header : '修改日期',
			 width : 200,
			 dataIndex : 'time',
			 sortable : true,
             align : 'center'
		 },{
			 header : '作者',
			 width : 100,
			 dataIndex : 'author',
			 sortable : true,
			 align : 'center'
         },{
			 header : '模型种类',
			 width : 100,
			 dataIndex : 'groupId',
			 sortable : true,
			 align : 'center'
         }]
          );
//模型版本信息列表
var versioninfoList=new Ext.grid.EditorGridPanel({
	id:'epcversion',
	title: "版本历史",
	region:'center',
	ds :epcVersioninfoStore,	//绑定store
	enableColumnResize : true,
	border : true,
	//clicksToEdit:1,
	enableColumnMove : true,
	enableHdMenu : false,
	stripeRows: true, //显示斑马线
	loadMask: true,
	autoHeight:true,
	bodyStyle:'width:100%',
    autoWidth:true,
    editable:true,
    sm : epcVersioninfosm,
    bbar: new Ext.PagingToolbar({//下面的分页条
        pageSize: 15,
        store: epcVersioninfoStore,
        displayInfo: true,
        displayMsg: '当前显示第 {0} 条到 {1} 条记录，一共 {2} 条',
        emptyMsg: "没有记录",
        lastText:"尾页",   
        nextText :"下一页",   
        prevText :"上一页",   
        firstText :"首页",   
        refreshText:"刷新页面"        
    }),
	cm : versioninfoColumnModel,
	store : roleVersioninfoStore
});
var versioninfoTabPanel = new Ext.TabPanel({
	border : true,
	region:'south',
	autoScroll:true,
	//clicksToEdit:1,
	//autoHeight:true,
    //autoWidth:true,
	height:200,
	items:[ versioninfoList,
	{
	title: "业务流程具体信息",
	region:'center',
	
	collapsible : true,
	border:false,
	contentEl:'epcDetail'
	}]
	,
	activeItem: 1
	});

var epc = new Ext.Panel({
	id : 'epc-panel',
	title : '业务流程视图',
	layout : 'border',
	//closable:true,
	defaults : {

		bodyStyle : 'padding:15px'
	},


	items : [ versioninfoTabPanel,{
		region : 'center',
		autoScroll : true,

		border : false,
		contentEl : 'epcCanvas'
	} ]
});


function showEpcContent(text) {
	
	var epcStore = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : 'showEpc.action?method=showEpc',
			method : 'post'

		}),
		reader : new Ext.data.JsonReader({
			root : "Epc",
			totalProperty : "totalEpc",
			id : "id"
		}, [ {
			name : 'id'
		}, {
			name : 'type'
		}, {
			name : 'name'
		}, {
			name : 'participant'
		}, {
			name : 'next'
		},
		{
			name : 'uid'
		}])
	});
	
	epcVersioninfoStore.load({
    	params:{
    		text:text
    	},
    	callback: function(r, options, success) { 
    		if (success == false) { 
    		alert("加载epc-version数据失败，无对应数据或者系统出现异常！"); 
    		}
    		else{
    		//alert("加载epc-version数据成功！");	
    		}
    	}
    });
	
	epcStore.load({
		params : {
			text : text
		},
		callback: function(r, options, success) { 
		if (success == false) {  
		alert("加载数据失败，无对应数据或者系统出现异常！"); 
		}else{ 			       
					var canvas=document.getElementById('epcCanvas');
					var ctx=canvas.getContext('2d');
					
					drawEpcModel(50,150,ctx,epcStore,canvas);
					Ext.getDom('epcDetail').innerHTML = "";
					 var cm=new Ext.grid.ColumnModel([
					              					new Ext.grid.RowNumberer(),
					              					{header:'编号',dataIndex:'id'},
					              					{header:'类型',dataIndex:'type'},
					              					{header:'名称',dataIndex:'name'},
					              					{header:'参与者',dataIndex:'participant'},
					              					{header:'下一结点',dataIndex:'next'},
					              					{header:'uid',dataIndex:'uid'}
					              				]);
					              			var grid=new Ext.grid.GridPanel({
					              					renderTo:'epcDetail',
					              					 viewConfig: {
					              			            forceFit:true
					              			        },
					              					layout:'fit',
					              					autoScroll:true,
					              					stripeRows: true,
					              					height:180,
					              					//autoHeight:true,
					              					loadMask:true,
					              					store: epcStore,					            
					              					cm:cm
					              					,			              						
					              					listeners : {
					              						cellclick : {
					              						fn : function(grid, rowIndex, columnIndex, e) {
					              							//点击记录，在表单中显示记录信息
					              							selected_row = rowIndex;
					              						var record = grid.getStore().getAt(rowIndex);
					              				    	 if (clickEpcStore.getAt(rowIndex).get('type')=='participant'){
					              				    		 var roleStore = new Ext.data.Store({
					              					    			proxy : new Ext.data.HttpProxy({
					              					    				url : 'gotoRole.action?method=gotoRole',
					              					    				method : 'post'
					              					    				
					              					    			}),
					              					    		reader : new Ext.data.JsonReader({
					              							root : "Role",
					              							totalProperty : "totalRole",
					              							id : "id"
					              								
					              								
					              						}, [ {
					              							name : 'id'
					              						}, {
					              							name : 'enName'
					              						}, {
					              							name : 'cnName'
					              						}, {
					              							name : 'description'
					              						}, {
					              							name : 'rolePackage'
					              						},{
					              							name:'parentRoleId'
					              							},
					              						{
					              							name : 'roleType'
					              						}, 
					              						{
					              							name: 'departmentid'
					              						},{
					              							name : 'fileId'
					              						} ])
					              					});
					              				    		 var uid=clickEpcStore.getAt(rowIndex).get('uid');
					              				    		 //alert (i);
					              					    		roleStore.load({
					              									params : {
					              										uid : uid
					              									},
					              										callback: function(r, options, success) { 
					              					                       if (success == false) {  
					              					                           alert("加载数据失败，无对应数据或者系统出现异常！"); 
					              					                       }else{ 
					              					                    	   var epcRecord=roleStore.getAt(0);
					              					                    	   showRoleContent(epcRecord);
					              					                   		  
					              					                   		   Ext.getCmp('content-panel').addTab('role-panel');	
					              						
					              						              						
					              						              					}
					              						              				
					              						              				}
					              					    		});
					              						}

					              				    	 else if (clickEpcStore.getAt(rowIndex).get('type')=='function'){
					              				    		 var functionStore = new Ext.data.Store({
					              					    			proxy : new Ext.data.HttpProxy({
					              					    				url : 'gotoFunctionpoint.action?method=gotoFunctionpoint',
					              					    				method : 'post'
					              					    				
					              					    			}),
					              					    		reader : new Ext.data.JsonReader({
					              							root : "Function",
					              							totalProperty : "totalFunctionpoint",
					              							id : "id"
					              					    		}, [ {
					              					    			name : 'id'
					              					    		}, {
					              					    			name : 'enName'
					              					    		}, {
					              					    			name : 'cnName'
					              					    		}, {
					              					    			name : 'description'
					              					    		}, {
					              					    			name : 'fileId'
					              					    		}] )
					              					});
					              				    		 var uid=clickEpcStore.getAt(rowIndex).get('uid');
					              				    		 //alert (i+'function-epc'+uid);
					              					    		functionStore.load({
					              									params : {
					              										uid : uid
					              									},
					              										callback: function(r, options, success) { 
					              					                       if (success == false) {  
					              					                           alert("加载数据失败，无对应数据或者系统出现异常！"); 
					              					                       }else{ 
					              					                    	   var epcRecord2=functionStore.getAt(0);
					              					                    	   showFunctionContent(epcRecord2.get('id'),epcRecord2.get('cnName'),'-1','-1');
					              					                   		  
					              					                   		   Ext.getCmp('content-panel').addTab('functionpoint-panel');	
					              						
					              						              						
					              						              					}
					              						              				
					              						              				}
					              					    		});
					              						}
					              				    	 
					              				    	 else if (clickEpcStore.getAt(rowIndex).get('type')=='event'){
					              				    		 var domainmodelStore = new Ext.data.Store({
					              					    			proxy : new Ext.data.HttpProxy({
					              					    				url : 'gotoDomainModel.action?method=gotoDomainModel',
					              					    				method : 'post'
					              					    				
					              					    			}),
					              					    		reader : new Ext.data.JsonReader({
					              							root : "DomainModel",
					              							totalProperty : "totalDomainModel",
					              							id : "id"
					              					    		}, [ {
					              					    			name : 'id'
					              					    		}, {
					              					    			name : 'enName'
					              					    		}, {
					              					    			name : 'cnName'
					              					    		}, {
					              					    			name : 'description'
					              					    		}, {
					              					    			name : 'type'
					              					    		}, {
					              					    			name : 'category'
					              					    		}, {
					              					    			name : 'fileId'
					              					    		}] )
					              					});
					              				    		 var uid=clickEpcStore.getAt(rowIndex).get('uid');
					              				    		 //alert (i+'domainmodel'+uid);
					              					    		domainmodelStore.load({
					              									params : {
					              										uid : uid
					              									},
					              										callback: function(r, options, success) { 
					              					                       if (success == false) {  
					              					                           alert("加载数据失败，无对应数据或者系统出现异常！"); 
					              					                       }else{ 
					              					                    	   var epcRecord3=domainmodelStore.getAt(0);
					              					                    	   showModelContent(epcRecord3.get('id'),epcRecord3.get('cnName'));
					              					                   		  
					              					                   		   Ext.getCmp('content-panel').addTab('domain-panel');	
					              						
					              						              						
					              						              					}
					              						              				
					              						              				}
					              					    		});
					              						}					              											              											              				       		              				    					              					              				       					              						              				   
					              						},
					              						scope : this
					              					}
					              				}
					              				});
				}
		}
	});
}